package com.adkfp.action;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adkfp.utils.MyUtils;
import com.adkfp.bean.ADKDeptBean;
import com.adkfp.bean.ADKNetGroupBean;
import com.adkfp.common.DeptNodes;
import com.adkfp.service.IADKDeptService;
import com.adkfp.service.IADKNetGroupService;
import com.adkfp.service.ManageUserService;

/**
 * class name:ADKDeptAction <BR>
 * class description: ADKDept表的增删改查 <BR>
 * Remark: <BR>
 * @version 1.00 2019年01月03日
 * @author JACK)jackwei
 */
@Controller
@RequestMapping("aDKDept")
public class ADKDeptAction {
	static String username;
	
	@Resource(name="aDKDeptService")
	private IADKDeptService aDKDeptService;
	
	@Resource(name="aDKNetGroupService")
	private IADKNetGroupService aDKNetGroupService;

	@Resource(name="manageUserService")
	private ManageUserService manageUserService;
  /**
 	*查询所有记录
 * @throws SQLException 
 * @throws ClassNotFoundException 
 * @throws IOException 
 	*/
	@RequestMapping("selectAll")
	@ResponseBody
	public Object selectAll(ADKDeptBean bean) throws ClassNotFoundException, SQLException, IOException{
		/*List<ADKDeptBean> list = new ArrayList<>();
		list = aDKDeptService.findByCondition(bean);
		long count =0;
		count = aDKDeptService.findByConditionCount(bean);
		return LayuiUtil.data(count,list);*/
		
		InputStream in =  this.getClass().getClassLoader().getResourceAsStream("/database.properties");
		Properties properties = new Properties();
		properties.load(in);
	

		//获取key对应的value值
		System.out.print("4444=================================");
		String dbdriver = properties.getProperty("jdbc.driverClass");//"com.mysql.jdbc.Driver");
		String dbhost =properties.getProperty("jdbc.jdbcUrl") ;//"jdbc:mysql://192.168.0.198:3306/qudao";
		String dbuser = properties.getProperty("jdbc.user","root");
		String dbpassword = properties.getProperty("jdbc.password", "123456");
		DeptNodes dt=new DeptNodes(dbdriver,dbhost,dbuser,dbpassword);
		return dt.getResult();
	}
	
	/**
	 *转发到页面 
	 */
	@RequestMapping("index")
	public String index() {
		return "aDKDept/aDKDeptIndex";
	}
	

	/**
	 * 部门表的增加（修改）
	 */
		@RequestMapping("deptInandUp")
		@ResponseBody
		public void deptInandUp(HttpServletRequest request,ADKDeptBean deptbean){
			
			//获取当前系统的用户名
			String userName=(String)request.getSession().getAttribute("USER_NAME_STRING");
			deptbean.setLoperator(userName);
			
			//自动获取当前时间
			String time=MyUtils.getNowDateTime();
			deptbean.setLdate(time);
			
			if(deptbean.getAutoId()!=null && deptbean.getAutoId()!=0) {
				//修改
				aDKDeptService.update(deptbean);
			}else {
				//增加
				aDKDeptService.insert(deptbean);
			}
		}	
		
	/**
	 * 部门表的删除 <BR>
	 */
	@RequestMapping("delete")
	@ResponseBody
	public String delete(HttpServletRequest request, String id) {
		/*System.out.println("----id----"+autoId);*/
		if(id!=null&&!id.equals("")) {
			//aDKDeptService.delete(Integer.parseInt(id));
			aDKDeptService.delete(id);
			return "删除成功！";
			}else {
			return "删除失败！";
			}
		}	
	
	/**
	 *转发页面到添加(修改)部门页面 
	 */
	@RequestMapping("toAddDept")
	public String toAddDept(HttpServletRequest request, String id,HttpServletResponse response) {
		ADKDeptBean deptbean = new ADKDeptBean();	
		if(id != null && !id.equals("")){
			/*deptbean=aDKDeptService.findById(Integer.parseInt(id));*/
			deptbean=aDKDeptService.findById(id);
		}
		request.setAttribute("deptBean",deptbean);
		return "aDKDept/addAdkDept";
	}
	
	
	//查找所有部门的名称
	@RequestMapping("selectDpetName")
	@ResponseBody
	public Object selectDpetName(ADKDeptBean deptbean) {
		List<ADKDeptBean> list = new ArrayList<>();
		list = aDKDeptService.selectDpetName();
		return list;
	}	
	
	//查找部门层级
	@RequestMapping("selectLevel")
	@ResponseBody
	public Object selectLevel(ADKDeptBean deptbean,String id) {
		//System.out.println("哈哈哈哈哈哈哈哈"+id);
		List<ADKDeptBean> list = new ArrayList<>();
		list = aDKDeptService.selectLevel(id);
		return list;
	}	
	
	//验证名称唯一(FMY)
	@RequestMapping("checkSname")
	@ResponseBody
	public Object checkSname(ADKDeptBean bean,HttpServletRequest request) {
		String netgroupname=request.getParameter("netgroupname");
		List<ADKDeptBean> list = new ArrayList<>();
		long count = aDKDeptService.checkSname(bean);
		return count;
	}
}